package hxs.extras;

import flash.events.IEventDispatcher;
import hxs.extras.FlashSignal;
/**
 * ...
 * @author Tony Polinelli
 */

class FlashEventsBase 
{
	var target:IEventDispatcher;
	var signals:Hash<FlashSignal<Dynamic>>;
	
	public function new(target:IEventDispatcher) 
	{
		this.target = target;
		
		// register to flash store if doesnt already exist
		// otherwise reference stored 'signals' hash
		// this allows for common store of signals, but also 
		// for instantiation of wrappers, for custom events.
		if (!FlashEventsStore.objects.exists(target))
		{
			FlashEventsStore.objects.set(target, this);
			signals = new Hash();
		} else {
			// if exists reference common signals store
			signals = FlashEventsStore.objects.get(target).signals;
		}
	}
	
	private inline function getNativeSignal(event:String):FlashSignal<Dynamic>
	{
		if (!signals.exists(event))
			signals.set(event, new FlashSignal(target, event));
		
		return cast signals.get(event);
	}
	
	public inline function removeAll()
	{
		for (s in signals)
			s.removeAll();
	}
	
}
